package net.guoyixian.admin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.Order.Direction;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import net.guoyixian.admin.pojo.Store;
import net.guoyixian.admin.service.StoreService;
import net.guoyixian.common.web.DataGrid;
import net.guoyixian.common.web.GyxResult;
import net.guoyixian.common.web.PageUtils;

@Controller
@RequestMapping("store")
public class StoreController extends BaseController {

	@Autowired
	private StoreService storeService;
	private static final ObjectMapper MAPPER = new ObjectMapper();

//	@RequiresAuthentication
	@RequiresPermissions("store:query")
	@RequestMapping("list")
	public String list() {
		int userid = super.getCurrentUserId();
		String username = super.getCurrentUserName();
		String host = super.getHost();
		return "store/storelist";
	}

	@RequiresPermissions("store:query")
	@RequestMapping("query")
	@ResponseBody
	public DataGrid query(Integer page, Integer rows, String sort, String order, String storeName, Integer audit) {
		page = (page == null) ? 1 : page;
		rows = rows > 0 ? rows : PageUtils.LIMIT;
		Direction direction = Direction.valueOf(order.toUpperCase(Locale.US));
		Order _order = new Order(PageUtils.LowerCaseWithUnder(sort), direction, null);
		List<Order> orders = new ArrayList<Order>();
		orders.add(_order);
		PageBounds bounds = new PageBounds(page, rows, orders);
		PageList<Store> list = storeService.queryList(bounds, storeName, audit);
		DataGrid dataGrid = new DataGrid();
		dataGrid.setRows(list);
		dataGrid.setTotal(list.getPaginator().getTotalCount());
		return dataGrid;
	}

	@RequestMapping("add")
	public String add() {
		return "store/storeadd";
	}

	@RequestMapping("queryAccount")
	@ResponseBody
	public GyxResult queryAccount(String account) {
		Integer total = storeService.queryAccount(account);
		if (total > 0) {
			return GyxResult.build(201, "该账户已注册!");
		}
		return GyxResult.ok();
	}

	@RequestMapping("save")
	@ResponseBody
	public GyxResult save(Store store, HttpServletRequest request, HttpServletResponse response) {
		storeService.save(store);
		return GyxResult.ok();
	}

	@RequestMapping("delete")
	@ResponseBody
	public GyxResult delete(String ids, HttpServletRequest request, HttpServletResponse response) {
		storeService.deleteByIDS(ids);
		return GyxResult.ok();
	}

	// @RequestMapping("login")
	// public ModelAndView login(HttpServletRequest request, HttpServletResponse
	// response) {
	// ModelAndView mv = new ModelAndView("login");
	// return mv;
	// }
	// /**
	// * 用户登录
	// *
	// * @param modelMap
	// * @return ResponseBody注解用于返回json,xml等文本数据
	// */
	// @RequestMapping(value = "/user/dologin")
	// @ResponseBody
	// public GyxResult dologin(@RequestParam("username") String username,
	// @RequestParam("password") String password,
	// HttpServletRequest request, HttpServletResponse response,ModelMap model)
	// {
	//
	// String ticket = this.userService.queryByUsername(username, password);
	// // 用户名或者密码错误
	// if (null == ticket) {
	// return GyxResult.build(201, "用户名或密码错误!");
	// }
	// CookieUtils.setCookie(request, response, TT_TICKET, ticket,
	// 60 * 60 * 24);
	// return GyxResult.ok();
	// }
	//
	// @RequestMapping(value = "/user/logout")
	// public String logout(HttpServletRequest request, HttpServletResponse
	// response) throws IOException {
	// String ticket ="TICKET_"+CookieUtils.getCookieValue(request,
	// StoreController.TT_TICKET);
	// // ticket="123";
	//
	// if (StringUtils.isNotBlank(ticket)) {
	// this.userService.logout(ticket);
	// CookieUtils.deleteCookie(request, response, StoreController.TT_TICKET);
	// }
	// return "login";
	// // response.sendRedirect("login");
	// }
}